package com.kaiwukj.android.ufamily.mvp.ui.page.service.fee;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.ListPopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.BaseSwipeBackActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.contract.PayFeeContract;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.PayInfo;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.PayFeeListResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.PayFeeResult;
import com.kaiwukj.android.ufamily.mvp.ui.adapter.PayFeeHistoryAdapter;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huiwings on 2019/10/25
 */
@Route(path = ARouterUrlKt.PayFeeHistory)
public class PayFeeHistoryActivity extends BaseSwipeBackActivity<PayFeePresenter> implements PayFeeContract.View {

    @BindView(R.id.toolbar)
    QMUITopBar qmuiTopBar;

    @BindView(R.id.container_refresh)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.qmui_empty_view)
    QMUIEmptyView emptyView;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    @BindView(R.id.tv_month)
    TextView tvMonth;
    @BindView(R.id.tv_type)
    TextView tvType;

    private PayFeeHistoryAdapter mAdapter;

    int type = 0;
    String payTime = "";

    @OnClick(R.id.container_month)
    void checkMonth() {
        new TimePickerBuilder(this, (date, v) -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH);
            tvMonth.setText(String.format("%d月", month + 1));
            payTime = TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd"));

            refreshLayout.autoRefresh();

            //ToastUtils.showShort("选择的月份是:" + month + 1);
        }).setType(new boolean[]{true, true, false, false, false, false})
                .setCancelColor(ContextCompat.getColor(this, R.color.common_text_slight_color))
                .setSubmitColor(ContextCompat.getColor(this, R.color.app_color_theme))
                .setOutSideCancelable(true)
                .isCyclic(true).build().show();
    }

    @OnClick(R.id.container_type)
    void checkType() {
        String[] items = {"全部", "物业费", "停车费"};
        ListPopupWindow popupWindow = new ListPopupWindow(this);
        popupWindow.setAdapter(
                new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items));
        popupWindow.setWidth(ConvertUtils.dp2px(100));
        popupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
        popupWindow.setAnchorView(findViewById(R.id.container_type));
        // popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_window_selector));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            popupWindow.setDropDownGravity(Gravity.CENTER_HORIZONTAL);
        }
        popupWindow.setVerticalOffset(ConvertUtils.dp2px(2));
        popupWindow.setModal(true);//模态框，设置为true响应物理键

        popupWindow.setOnItemClickListener((parent, view, position, id) -> {
            //ToastUtils.showShort("index:" + position);

            type = position;//
            tvType.setText(items[position]);
            refreshLayout.autoRefresh();

            popupWindow.dismiss();
        });

        popupWindow.show();

    }


   /*// @Override
    //protected int getLayoutId() {
        return R.layout.activity_payfee_history;
    }*/


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        /*DaggerPayFeeComponent.builder()
                .appComponent(appComponent)
                .payFeeModule(new PayFeeModule(this))
                .build()
                .inject(this);*/
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_payfee_history;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        qmuiTopBar.setTitle("缴费记录");
        qmuiTopBar.addLeftBackImageButton().setOnClickListener(v -> onBackPressedSupport());

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new SpaceItemDecoration(1));

        mAdapter = new PayFeeHistoryAdapter();

        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) ->
                ARouter.getInstance().build(ARouterUrlKt.PayFeeHistoryDetail)
                        .withSerializable("result", mAdapter.getItem(position)).navigation());

        refreshLayout.autoRefresh();

        refreshLayout.setOnRefreshListener(refreshLayout -> {
            request();
            emptyView.setLoadingShowing(true);
        });


        //requestData();
    }


    private void request() {
        if (StringUtils.isEmpty(payTime)) {
            payTime = TimeUtils.date2String(new Date(), new SimpleDateFormat("yyyy-MM-dd"));
        }
       // mPresenter.requestPayFeeList(type, payTime);//
    }

    @Override
    public Context getCtx() {
        return null;
    }

    @Override
    public void getPayFeeList(List<PayFeeListResult> data) {
        /*refreshLayout.finishRefresh();
        if (data.size() > 0) {

            //data.get(0).setSelected(true);
            emptyView.hide();
            mAdapter.setNewData(data);
        } else {
            showMessage("您没有需缴费的账单");
            // new Handler().postDelayed(this::finish, 800);
        }*/
    }

    @Override
    public void showPayFeeList(List<PayFeeResult> results) {
        refreshLayout.finishRefresh();
        if (results.size() > 0) {

            //data.get(0).setSelected(true);
            emptyView.hide();
            mAdapter.setList(results);
        } else {
            mAdapter.clear();
            showMessage("没有查询到账单");
            // new Handler().postDelayed(this::finish, 800);
        }
    }

    @Override
    public void getListFail() {

    }

    @Override
    public void wxPayGetInfo(PayInfo data) {

    }

    @Override
    public void showMessage(@NonNull String message) {
        refreshLayout.finishRefresh();
        super.showMessage(message);
        emptyView.hide();
    }

    @Override
    public void post(Runnable runnable) {

    }
}
